// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Item {
    id: item1
    width: 400
    height: 500

    Rectangle {
        id: rectangle_bg
        color: "#00276b"
        anchors.fill: parent
    }

//    StatusBar
//    {
//        id: status_bar
//        x:0
//        y:0
//    }

    Loader
    {
        id: loader_status
        source:""

        function load(color, status)
        {
            source = "";
            source = "StatusBar.qml";
            item.show(color, status);
        }
    }

    TopMenu {
        id: topmenu1
        x: 0
        anchors.top: loader_status.bottom
        anchors.topMargin: 0
        state_text: "hide"
        button0_text: "位置搜索"
        button1_text: "公交路线导航"
        button2_text: "公交站点查询"
        button3_text: "驾车路线导航"
        title_text: "位置搜索"

        onButton0_click:
        {
            pageloader.load_search();
            title_text = "位置搜索";
            topmenu1.hide();
        }

        onButton1_click:
        {
            pageloader.load_busnavi();
            title_text = "公交路线导航";
            topmenu1.hide();
        }

        onButton2_click:
        {
            pageloader.load_busstation();
            title_text = "公交站点查询";
            topmenu1.hide();
        }

        onButton3_click:
        {
            pageloader.load_carnavi();
            title_text = "驾车路线导航";
            topmenu1.hide();
        }
    }

//    SearchPoi {
//        id: searchpoi1
//        x: 0
//        anchors.top: topmenu1.bottom
//        anchors.topMargin: 0
//    }

    Loader
    {
        id: pageloader
//        item.opacity: 1
//        item.objectName: "loaderitem"
//        item.height: height
//        item.width: width
//        item.visible: true
//        item.enabled: true
        anchors.top: topmenu1.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.topMargin: 0
        source: "SearchPoi.qml"

        function load_search()
        {
            source = "SearchPoi.qml";
        }

        function load_busnavi()
        {
            source = "BusNavi.qml";
        }

        function load_busstation()
        {
            source = "BusStation.qml";
        }

        function load_carnavi()
        {
            source = "CarNavi.qml";
        }

        function hide()
        {
            item.hide();
        }
    }

    Rectangle
    {
        id: loading_cover
//        anchors.fill: parent
        x:0
        y:0
        width: parent.width
        height: parent.height
        color: "black"
        opacity: 0.8

        Behavior on height
        {
            NumberAnimation{ easing.type: Easing.InQuad}
        }

        Behavior on opacity
        {
            NumberAnimation{ easing.type: Easing.InQuad}
        }

        Text {
            id: text1
            x: 176
            y: 243
            color: "#ffffff"
            text: "地图加载中<br>请稍后。。。"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 44
        }
    }

    Data
    {
        id: data_map
    }

    Connections
    {
        id: conn_backend
        target: backend

        onMap_load_finish:
        {
            loading_cover.height = 0;
            loading_cover.opacity = 0;
        }

        onSearch_result:
        {
            console.log("status and time : " + result.status + " , " + result.list.length);
            if (result.status == "E0")
            {
                loader_status.load("darkgreen", "搜索成功");
                data_map.clear_model_poi();
                pageloader.hide();
                for (var i = 0; i < result.list.length; i++)
                {
                    data_map.add_model_poi(result.list[i].distance
                                           ,result.list[i].pguid
                                           ,result.list[i].address
                                           ,result.list[i].tel
                                           ,result.list[i].name
                                           ,result.list[i].srctype
                                           ,result.list[i].type
                                           ,result.list[i].y
                                           ,result.list[i].x
                                           );
                }
            }
            else if (result.status == "E0")
            {
                loader_status.load("darkorange", "搜索没有结果");
                data_map.clear_model_poi();
            }
            else
            {
                loader_status.load("darkred", "网络异常，请重试");
                data_map.clear_model_poi();
            }
        }

        onBus_navi_result:
        {
            console.log("status and time : " + result.status + " , " + result.time);
            if (result.status == "E0")
            {
                loader_status.load("darkgreen", "搜索成功");
                data_map.clear_model_busnavi();
                pageloader.hide();
                for (var i = 0; i < result.list.length; i++)
                {
//                    console.log(JSON.stringify(result.list[i].segmentList));
                    data_map.add_model_busnavi(result.list[i].bounds
                                           ,result.list[i].footEndLength
                                           ,JSON.stringify(result.list[i].segmentList)
                                           );
                }
            }
            else if (result.status == "E0")
            {
                loader_status.load("darkorange", "搜索没有结果");
                data_map.clear_model_busnavi();
            }
            else
            {
                loader_status.load("darkred", "网络异常，请重试");
                data_map.clear_model_busnavi();
            }
        }

        onBus_sta_result:
        {
            console.log("status and time : " + result.status + " , " + result.time);
            if (result.status == "E0")
            {
                loader_status.load("darkgreen", "搜索成功");
                data_map.clear_model_busstation();
                pageloader.hide();
                for (var i = 0; i < result.list.length; i++)
                {
                    data_map.add_model_busstation(result.list[i].terminal_name
                                           ,result.list[i].line_id
                                           ,result.list[i].type
                                           ,result.list[i].end_time
                                           ,result.list[i].name
                                           ,result.list[i].company
                                           ,result.list[i].length
                                           ,result.list[i].start_time
                                           );
                }
            }
            else if (result.status == "E8008")
            {
                loader_status.load("darkorange", "搜索没有结果");
                data_map.clear_model_busstation();
            }
            else
            {
                loader_status.load("darkred", "网络异常，请重试");
                data_map.clear_model_busstation();
            }
        }

        onBus_line_result:
        {
            console.log("status and time : " + result.status + " , " + result.time);
            //console.log(JSON.stringify(result));
            if (result.status == "E0")
            {
                loader_status.load("darkgreen", "搜索成功");
                data_map.clear_model_busline();
                for (var i = 0; i < result.list[0].stationdes.length; i++)
                {
                    //console.log(result.list[i].xy);
                    data_map.add_model_busline(result.list[0].stationdes[i].stationNum
                                           ,result.list[0].stationdes[i].name
                                           ,result.list[0].stationdes[i].code
                                           ,result.list[0].stationdes[i].xy.split(",")[0]
                                           ,result.list[0].stationdes[i].xy.split(",")[1]
                                           );
                }
            }
            else if (result.status == "E0")
            {
                loader_status.load("darkorange", "搜索没有结果");
                data_map.clear_model_busline();
            }
            else
            {
                loader_status.load("darkred", "网络异常，请重试");
                data_map.clear_model_busline();
            }
        }

        onCar_navi_result:
        {
            console.log("status and time : " + result.status + " , " + result.time);
            if (result.status == "E0")
            {
                loader_status.load("darkgreen", "搜索成功");
                data_map.clear_model_car();
                pageloader.hide();
                for (var i = 0; i < result.list.length; i++)
                {
                    data_map.add_model_car(result.list[i].locationCode
                                           ,result.list[i].form
                                           ,result.list[i].coor
                                           ,result.list[i].direction
                                           ,result.list[i].roadLength
                                           ,result.list[i].action
                                           ,result.list[i].grade
                                           ,result.list[i].textInfo
                                           ,result.list[i].accessorialInfo
                                           ,result.list[i].roadName
                                           ,result.list[i].driveTime
                                           );
                }
            }
            else if (result.status == "E0")
            {
                loader_status.load("darkorange", "搜索没有结果");
                data_map.clear_model_car();
            }
            else
            {
                loader_status.load("darkred", "网络异常，请重试");
                data_map.clear_model_car();
            }
        }

        onPoi_start:
        {
            console.log("start poi: " + poi);
            var arry = poi.split(",");
            data_map.start_x = arry[0];
            data_map.start_y = arry[1];
            data_map.start_name = "地图上的起点";
        }

        onPoi_end:
        {
            console.log("end poi: " + poi);
            var arry = poi.split(",");
            data_map.end_x = arry[0];
            data_map.end_y = arry[1];
            data_map.end_name = "地图上的终点";
        }

        onCity_code:
        {
            console.log("city code: " + code);
            data_map.citycode = code;
        }
    }
}
